/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.teotigraphix.dx.patterns.manager.meta
{

/**
 * @private
 * 
 * The superclass for all event handler metadata.
 * 
 * <p>This metadata binds events from the Component to Behavior, Part to Behavior 
 * or the Service to Behavior.</p>
 * 
 * <p>The metadata arguments;</p>
 * <ul>
 * <li><code>event</code> : The event dispatched from the bound property.</li>
 * <li><code>handler</code> : The handler method name of the event.</li>
 * <li><code>phase</code> : The listener's phase.</li>
 * <li><code>priority</code> : The listener's priority.</li>
 * <li><code>weak</code> : Whether to make this listener a weak reference.</li>
 * </ul>
 * 
 * <p><em>This class is internal to the toolkit.</em></p>
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 * @date 12-04-08
 */
public class EventHandlerInfo
{
	include '../../../core/Version.as'
	
	//--------------------------------------------------------------------------
	//
	//  Public :: Variables
	//
	//--------------------------------------------------------------------------
	
	/**
	 * The dispatcher of the event.
	 * 
	 * <p>The variable or accessor bound to the metadata.</p>
	 */	
	public var dispatcher:String;
	
	/**
	 * The event name being bound to be dispatched by the dispatcher.
	 */
	public var event:String;
	
	/**
	 * The handler method of the event being dispatched.
	 */
	public var handler:String;
	
	/**
	 * The listener's phase.
	 * 
	 * <p>Possible values;</p>
	 * <ul>
	 * <li><code>bubble</code></li>
	 * <li><code>capture</code></li>
	 * <li><code>both</code></li>
	 * </ul>
	 */
	public var phase:String = "bubble"; // bubble, capture, both
	
	/**
	 * The listener's priority.
	 */
	public var priority:int = 0;
	
	/**
	 * Whether to make this listener a weak reference.
	 */	
	public var weak:Boolean = false;
	
	//--------------------------------------------------------------------------
	//
	//  Private :: Variables
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Whether this event handler is a skin part of component.
	 */
	private var _isClassHandler:Boolean = false;
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param reflection An XML description of the element.
	 * @param isClassHandler A boolean indicating whether this event handler
	 * is a class level handler or a part level handler.
	 */
	public function EventHandlerInfo(reflection:XML, isClassHandler:Boolean)
	{
		_isClassHandler = isClassHandler
		var ph:String;
		if (_isClassHandler)
		{
			dispatcher = "component";
			event = reflection.arg.(@key == "event").@value;
			handler = reflection.arg.(@key == "handler").@value;
			
			ph = reflection.arg.(@key == "phase").@value;
			if (ph == "capture" || ph == "both")
				phase = ph;
			
			priority = int(reflection.arg.(@key == "priority").@value);
			weak = (reflection.arg.(@key == "weak").@value == "true") ? true : false;
		}
		else
		{
			dispatcher = reflection.parent().@name;
			if (dispatcher == "" || dispatcher == null)
			{
				dispatcher = reflection.arg.(@key == "id").@value;
			}
			event = reflection.arg.(@key == "event").@value;
			handler = reflection.arg.(@key == "handler").@value;
			
			ph = reflection.arg.(@key == "phase").@value;
			if (ph == "capture" || ph == "both")
				phase = ph;
			
			priority = int(reflection.arg.(@key == "priority").@value);
			weak = (reflection.arg.(@key == "weak").@value == "true") ? true : false;				
		}
	}
	
	//--------------------------------------------------------------------------
	//
	//  Public API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Returns whether this is a class handler (true) or a part handler (false).
	 */
	public function isClassHandler():Boolean
	{
		return _isClassHandler;
	}
	
	/**
	 * Returns whether the passed info is equal to this instance.
	 * 
	 * @param info The instance to test for equality.
	 * @return A boolean indicating whether this instance is equal.
	 */
	public function equals(info:EventHandlerInfo):Boolean
	{
		return (
			_isClassHandler == info.isClassHandler() &&
			dispatcher == info.dispatcher &&
			event == info.event &&
			handler == info.handler &&
			phase == info.phase &&
			priority == info.priority &&
			weak == info.weak
		);
	}
}
}